<?php
/**
 * Created by PhpStorm.
 * User: My
 * Date: 2018/10/15
 * Time: 13:03
 */

namespace App\Repositories;

use App\Http\Helper\BeanHelper;
use App\Models\TestingChannel;

class TestingChannelRepository extends Repository
{
    const STATUS_UNUSED = 0;
    const STATUS_ACTIVE = 1;

    /**
     * 根据code查询渠道
     * @param $code
     * @return mixed
     */
    public function getOneByCode($code) {
        return BeanHelper::convertStdClsToArr(
            TestingChannel::where([
                'code' => $code,
                'status' => self::STATUS_ACTIVE
            ])->first()
        );
    }

    /**
     * 获取所有的渠道信息
     * @return mixed
     */
    public function getAll() {
        return BeanHelper::convertStdClsToArr(
            TestingChannel::all()
        );
    }

    /**
     * 创建一个渠道
     * @param $data
     * @return bool
     */
    public function store($data) {
        $channel = $this->getOneByCode($data['code']);
        if ($channel) {
            return false;
        }
        return TestingChannel::firstOrCreate($data);
    }

    /**
     * 修改一个渠道
     * @param $data
     * @return bool
     */
    public function update($data) {
        if ($data['id']) {
            return TestingChannel::where('id', $data['id'])->update($data);
        }
        return false;
    }

    public function model()
    {
        // TODO: Implement model() method.
    }

    public function tag()
    {
        // TODO: Implement tag() method.
    }
}